package org.apache.ibatis.plugin;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.beans.Statement;
import java.util.Properties;

/**
 * @author YCL
 * @version 1.0
 * @Description
 * @date 2021/11/1
 */
@Intercepts({
  @Signature(type= Executor.class,method = "update", args = {MappedStatement.class,Object.class}),
  @Signature(type= Executor.class,method = "query",  args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
@Slf4j
public class MyPlugin implements Interceptor{

  private Properties properties ;

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    log.info("自定义属性myProperties={}",properties);
    // 拦截sql
    Object[] args = invocation.getArgs();
    MappedStatement statement = (MappedStatement) args[0];
    Object parameterObject = args[1];
    BoundSql boundSql = statement.getBoundSql(parameterObject);
    String sql = boundSql.getSql();
    log.info("获取到的SQL：{}"+sql);
    Object proceed = invocation.proceed();
    return proceed;
  }

  @Override
  public Object plugin(Object target) {
    return Interceptor.super.plugin(target);
  }

  @Override
  public void setProperties(Properties properties) {
    //Interceptor.super.setProperties(properties);
    this.properties = properties;
  }
}
